/* List clicked View */
function getSystemInfo(id){
	var url = "getSystemInfo.jsp";
	var param = {
		work_no : id
	};
	callAjax(url,param, "SYSTEM_VIEW");
}

/* Log List clicked Popup View */
function system_detail(id){
	var url = "system_detail.jsp";
	var param = {
		ip_addr : id
	};
	openModal(url, param, 416, 408);
}

/* callAjax successAjax */
function successAjax(data, serviceName){
	if(serviceName == "SYSTEM_VIEW"){
		$('#system_name').val(data.rows[0].system_name);
		$('#system_id').val(data.rows[0].system_id);
		$('#system_kind').val(data.rows[0].system_kind);
		$('#os_version').val(data.rows[0].os_version);
		$('#sid').val(data.rows[0].sid);
		$('#ip_addr').val(data.rows[0].ip_addr);
		$('#connect_way').val(data.rows[0].connect_way);
		$('#connect_port').val(data.rows[0].connect_port);
		$('#data_req_res').val(data.rows[0].data_req_res);
		$('#req_res_port').val(data.rows[0].req_res_port);
		$('#admin_account').val(data.rows[0].admin_account);
		$('#system_desc').val(data.rows[0].system_desc);
		$('#create_date').val(data.rows[0].create_date);
	}else if(serviceName == "SYSTEM_DEL"){
		alert(data.result);
	}else if(serviceName == "SYSTEM_INFO"){
		alert(data.result);
	}
}

/* JqGrid System List */
function initGridSystem() {
	$("#gridList").jqGrid({
		url:'getJqgridSystemList.jsp',
		mtype: 'post',
		//postData: {  },
		datatype: "json",
		colNames:['구분','시스템','IP주소','관리계정','접속방법'],
		colModel:[
				{name:'type',index:'type', width:50,align:"right",key:true},
				{name:'system',index:'system', width:80,align:"left"},
				{name:'ip_addr',index:'ip_addr', width:180,align:"left"},
				{name:'admin_account',index:'admin_account', width:100,align:"right"},
				{name:'connect_way',index:'connect_way', width:50,align:"right"}
			],
		rowList: [0,5,10,15,20],
		rowNum:5,
		height:240,
		autowidth : true,
		sortname: "system",
		sortorder: 'DESC',
		viewrecords: true,
		multiselect: true,//앞에 체크박스처리
		multikey: "",//multikey: "ctrlKey/shiftKey",
		pager: '#pager_id',
      
		onSelectRow: function(id){  //row가 선택되었을 경우
			getSystemInfo(id);
		},
		loadComplete : function(data){
			pagecount = data.total;
			$("#pager").pager({ pagenumber: data.page, pagecount: pagecount, buttonClickCallback: PageClick });
        },
		loadError : function(xhr,st,err) {
			//jQuery("#rsperror").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
		},
		emptyrecords: "데이터가 존재하지 않습니다.",
		caption:'테이블',
		jsonReader: {
			repeatitems:false
		}
	});
	$("#gridList").jqGrid('navGrid','#pager_id',{edit:false,add:false,del:false,search:true});
}

/* JqGrid SystemLog List */
function initGridSystemLog() {
	$("#gridList2").jqGrid({
		url:'getJqgridSystemLogList.jsp',
		mtype: 'post',
		//postData: {  },
		datatype: "json",
		colNames:['작업자','원격지','구분','작업','기간','결과','중요도'],
		colModel:[
				{name:'worker',index:'important', width:50,align:"right",key:true},
				{name:'ip_addr',index:'ip_addr', width:50,align:"left"},
				{name:'type',index:'type', width:50,align:"left"},
				{name:"work",index:'work', width:100,align:"right"},
				{name:'system_date',index:'system_date', width:50,align:"right"},
				{name:'result',index:'result', width:80,align:"right"},
				{name:'important',index:'important', width:150,align:"right"}
			],
		rowList: [0,5,10,15,20],
		rowNum:5,
		height:130,
		autowidth : true,
		sortname: "worker",
		sortorder: 'DESC',
		viewrecords: true,
		multiselect: false,//앞에 체크박스처리
		multikey: "",//multikey: "ctrlKey/shiftKey",
		pager: '#pager_id',
      
		onSelectRow: function(id){  //row가 선택되었을 경우
			var rowData = jQuery(this).getRowData(id); 
            var ip_addr= rowData['ip_addr'];//replace name with you column
            system_detail(ip_addr);
		},
		loadComplete : function(data){
			pagecount2 = data.total;
			$("#pager2").pager({ pagenumber: data.page, pagecount: pagecount2, buttonClickCallback: PageClick2 });
        },
		loadError : function(xhr,st,err) {
			//jQuery("#rsperror").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
		},
		emptyrecords: "데이터가 존재하지 않습니다.",
		caption:'테이블',
		jsonReader: {
			repeatitems:false
		}
	});
	$("#gridList2").jqGrid('navGrid','#pager_id',{edit:false,add:false,del:false,search:true});
}

var pagecount;
var pagecount2;

$(document).ready(function(){
	initGridSystem();
	initGridSystemLog();
	initButton();
});
PageClick = function(pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: pagecount, buttonClickCallback: PageClick });
    $("#gridList").setGridParam({ postData: { pagePlug: pageclickednumber} }).trigger("reloadGrid");  // Postdata을 넘겨주고 url로 지정된 ajax 호출하고 Grid을 Relo
};
PageClick2 = function(pageclickednumber) {
    $("#pager2").pager({ pagenumber: pageclickednumber, pagecount: pagecount2, buttonClickCallback: PageClick2 });
    $("#gridList2").setGridParam({ postData: { pagePlug: pageclickednumber} }).trigger("reloadGrid");  // Postdata을 넘겨주고 url로 지정된 ajax 호출하고 Grid을 Relo
};

function initButton() {
	
	var link = getUrl();
	//이동,암호변경,보안암호변경,추가,삭제 클릭 이벤트
	$("ul.btn_box > li > a").each(function( index ) {
		$(this).on("click", function(event){
			var name = $(this).attr("id");
			var param = {
					treeType : "test"
				};
			
			if( name == "btn_go" ) {
				//이동
				openModal(link+"system_move.jsp", param, 410, 0);
			} else if( name == "btn_change_pw" ) {
				/* 비밀번호 변경 */
				openModal(link+"system_change_passwd.jsp", param, 410, 0);
			} else if( name == "btn_change_pw2" ) {
				/* 보안 비밀번호 변경 */
				openModal(link+"system_change_secure_passwd.jsp", param, 410, 0);
			} else if( name == "btn_add" ) {
				/* 사용자등록  */
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 openModal(link+"system_add.jsp", param, 420, 0);
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다")
				 }
				
			} else if( name == "btn_del" ) {
				/* checkbox checked 삭제 */
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 callAjax("deleteSystem.jsp", param, "SYSTEM_DEL");
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다")
				 }

			} else if( name == "btn_del2" ) {
				/* checkbox checked 삭제 */
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 callAjax("deleteSystem.jsp", param, "SYSTEM_DEL");
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다")
				 }
			} else if( name == "btn_save" ) {
				/* 작업 상세보기에서 저장*/
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 var del_msg = confirm("저장하시겠습니까?");
						if( del_msg ) {
							 callAjax("insertSystemInfo.jsp", param, "SYSTEM_INFO");
						}
					
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다")
				 }

			}
		});
	});
}
